package com.yulebaby.callsenter.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.adapter.AddTargetAdapter;
import com.yulebaby.callsenter.bean.ResultBean;
import com.yulebaby.callsenter.bean.TranProgressTargetBean;
import com.yulebaby.callsenter.event.EventFactory;
import com.yulebaby.callsenter.event.RefreshEvent;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.task.UploadTransDailyTask;
import com.yulebaby.callsenter.ui.base.BaseActivity;
import com.yulebaby.callsenter.ui.view.YuLeBaByLinearLayoutManager;
import com.yulebaby.callsenter.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AddTodayTargetActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.rv_target)
    RecyclerView mRvTarget;

    private List<TranProgressTargetBean> beanList;
    private AddTargetAdapter mAdapter;
    private View mBtnSave;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_add_today_target;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        beanList = new ArrayList<>();
        for (int i = 2; i < 8; i++) {
            beanList.add(new TranProgressTargetBean(i, -1));
        }

        View view = LayoutInflater.from(this).inflate(R.layout.layout_addbutton, null);
        mBtnSave = view.findViewById(R.id.btn_common);

        mIvBack.setOnClickListener(this);
        mBtnSave.setOnClickListener(this);

        mAdapter = new AddTargetAdapter(beanList);
        mAdapter.addFooterView(view);
        mRvTarget.setHasFixedSize(true);
        mRvTarget.setLayoutManager(new YuLeBaByLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRvTarget.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_common:
                commitTranProgress();
                break;
        }
    }

    private void commitTranProgress() {
        boolean flag = true;
        for (TranProgressTargetBean bean : beanList) {
            if (bean.getTran_targetnum() == -1 || bean.getTran_targetnum() == -2) {
                bean.setTran_targetnum(-2);
                flag = false;
            }
        }
        if (flag) {
            UploadTransDailyTask.doUploadPreSaleRemark(this, beanList, new HttpCallback<ResultBean>() {
                @Override
                public void onSuccessResultNull() {
                    super.onSuccessResultNull();
                    EventBus.getDefault().post(EventFactory.CreateRefreshEvent(RefreshEvent.refreshDailyData));
                    finish();
                }
            });
        } else {
            ToastUtil.showToast("转化目标不符合规则，请重新填写");
            mAdapter.notifyDataSetChanged();
        }
    }
}
